package git.soulbgm.hbase;

import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HConstants;
import org.apache.hadoop.hbase.client.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.IOException;

/**
 * @author SoulBGM
 * @version 1.0
 * @date 2020/6/18 10:24
 */
@Slf4j
@Component
public class HBaseConnectionFactory {

    @Value("${hbase.zookeeper.quorum}")
    private String zkQuorum;

    @Value("${hbase.master}")
    private String hbaseMaster;

    @Value("${hbase.zookeeper.property.clientPort}")
    private String zkPort;

    @Value("${zookeeper.znode.parent}")
    private String znode;

    private static Configuration conf;

    private static Connection connection;

    @PostConstruct
    public void setProperties() {
//        System.setProperty("hadoop.home.dir", System.getenv("HADOOP_HOME"));
        System.setProperty("hadoop.home.dir", "D:/software/hadoop-common-2.2.0-bin-master");
        conf = HBaseConfiguration.create();
        /*conf.set("hbase.zookeeper.quorum", zkQuorum);
        conf.set("hbase.master", hbaseMaster);
        conf.set("hbase.zookeeper.property.clientPort", zkPort);
        conf.set("zookeeper.znode.parent", znode);*/
        conf.set(HConstants.ZOOKEEPER_QUORUM, "jjyy");
        conf.set(HConstants.ZOOKEEPER_CLIENT_PORT, "2181");
        conf.set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/hbase");
        conf.set(HConstants.HBASE_CLIENT_IPC_POOL_TYPE, "soul");
        conf.set(HConstants.HBASE_CLIENT_IPC_POOL_SIZE, "30");
        conf.set(HConstants.HBASE_CLIENT_OPERATION_TIMEOUT, "3000");
        try {
            connection = ConnectionFactory.createConnection(conf);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() {
        return connection;
    }
}
